<?php

namespace Frontend\Controllers;

use DuAdmin\Core\BaseController;
use DuAdmin\Helpers\AppHelper;
use DuAdmin\Hooks\FindSlugHook;
use Yii;
use yii\console\Exception;
use yii\web\NotFoundHttpException;

/**
 * Site controller
 */
class SiteController extends BaseController
{

    public function init()
    {
        parent::init();
        if (!Yii::$app->request->isAjax) {
            $this->view->registerMetaTag([
                'name'    => 'keywords',
                'content' => AppHelper::getSetting('site.keywords')
            ], 'keywords');
            $this->view->registerMetaTag([
                'name'    => 'description',
                'content' => AppHelper::getSetting('site.description')
            ], 'description');
        }
    }

    /**
     *
     * {@inheritdoc}
     */
    public function actions()
    {

        return [
            'error'         => [
                'class' => 'DuAdmin\Core\CustomErrorAction',
                'layout' => 'content'
            ],
            'captcha'       => [
                'class'           => '\yii\captcha\CaptchaAction',
                'offset'          => '0',
                'maxLength'       => 4,
                'minLength'       => 4,
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null
            ],
            'sms-captcha'        => [
                'class' => '\DuAdmin\Sms\SendSmsCaptchaAction'
            ],
            'mail-captcha'        => [
                'class' => '\DuAdmin\Mail\SendMailCaptchaAction'
            ],
            'dict' => [
                'class' => '\DuAdmin\Dict\DictAction',
            ],
            'upload'        => [
                'class' => '\DuAdmin\Uploader\LocalUploadAction'
            ],
            'upload-token'  => [
                'class' => '\DuAdmin\Uploader\TokenAction'
            ],
            'upload-delete' => [
                'class' => '\DuAdmin\Uploader\DeleteAction'
            ]
        ];
    }

    /**
     * Displays homepage.
     *
     * 检查是否有数据库级别的配置
     *
     *
     * @return string
     */
    public function actionIndex()
    {

        if ($url = AppHelper::getSetting("site.index-page")) {
            return $this->redirect($url);
        } else {
            return $this->render("index");
        }
    }

    /**
     * 显示页面
     *
     * @param string $slug
     * @return mixed|NULL|string
     * @throws NotFoundHttpException|Exception
     */
    public function actionPage($slug = 'index')
    {

        // try to display action from controller
        try {
            return $this->run('/' . $slug);
        } catch (\yii\base\InvalidRouteException $ex) {
            \Yii::debug($ex->getMessage());
        }
        // try to display action from application
        try {
            return \Yii::$app->runAction($slug . '/');
        } catch (\yii\base\InvalidRouteException $ex) {
        }
        // try to display static page from hook handler
        $hook = FindSlugHook::emit($this, [
            'slug' => $slug
        ]);
        if ($hook && $hook->payload) {
            return $hook->payload;
        }
        // if nothing suitable was found then throw 404 error
        throw new NotFoundHttpException('Page not found.');
    }
}
